<?php
session_start();
require_once("../config/include.inc.php");
require_once("../class/Common_Function.php");


//访问权限验证
$userRole = $_SESSION['user_role'];
if($userRole == 1 ||  $userRole == 5 || $userRole == 8){
	//管理员,订单管理员,任务管理员+订单管理员 + 团队成员
}else{
	if($userRole > 0){
		$url = "../index.php?loginError=2";//没权限
	}else{
		$url = "../index.php?loginError=3";//没登录
	}	
	echo "<script language='javascript' type='text/javascript'>";
	echo "window.location.href='$url'";
	echo "</script>";
}

$commonFunction = new Common_Function();


//**************新增订单******************************
$errorMsg = "";
$doFlag = $_POST["doFlag_addOrderForm"];
$order_id = $_POST["orderId_addOrderForm"];
if($doFlag == "order_H"){
	//加新订单 Hiteclaser
	//$order_id = $_POST["order_H"];
	$sql_orders = "select * from orders where orders_id=".$order_id;
	$result_orders = $db_H->Execute($sql_orders);
	if(!$result_orders->EOF){
		$str = "H";
		$sub_total = 0; //小计
		$shipping_fee = 0; //运费
		$order_total = 0; //总计
		$order_coupon = "";//coupon
		$order_no = "H".$order_id;
		$order_site = "hiteclaser";//来自哪个网店
		$products_info = '';//订单明细
		$order_status = 10;//当前状态: 默认没付款(详见 设置页面)
		$battery_flag = 0;//是否有电池 0=没电池 1=有电池
		$tracking_flag = 0;//是否转跟单 0=没跟单 1=有跟单
		$payment_flag = 0;//付款方式 0=PayPal 1=Bank
		$pay_time = ''; //付款时间
		$customer_name = ''; //客户名字
		$customer_telephone = ''; //客户电话
		$customer_address = ''; //客户地址
		$customer_postcode = ''; //客户邮编
		$customer_email = ''; //客户email
		$product_description = "LED手电筒 / LED Flashlight";//☆产品 数量 总价
		$product_description_num = 0;//产品 ☆数量 总价
		$product_description_price = "";//产品 数量 ☆总价
		$sum_orders = 0;//历史订单总数

		

		//$order_total = $result_orders->fields['order_total'];

		//------------满10000 免运费 订单号 2390 以后开始设置------------------------	
		/*
		if(($order_total - $shipping_fee) >= 10000 && $order_id > 2390){
			$shipping_fee = 0;
		}
		*/

		$sub_total = $order_total - $shipping_fee;
		$strTmp = $result_orders->fields['payment_method'];
		if($strTmp == '銀行振込'){
			$payment_flag = 1;
		}else{
			$payment_flag = 0;
		}
		$customer_name = $result_orders->fields['delivery_name'];
		$customer_telephone =  $result_orders->fields['customers_telephone'];
		
		$customer_address =  $result_orders->fields['delivery_street_address'].' '.$result_orders->fields['delivery_suburb'].', '.$result_orders->fields['delivery_city'].', '.$result_orders->fields['delivery_state'].', '.$result_orders->fields['delivery_country'];
		$customer_postcode = $result_orders->fields['delivery_postcode'];
		$customer_email = $result_orders->fields['customers_email_address'];



		$sql_orders_products = "select * from orders_products where orders_id=".$order_id;
		$result_orders_products = $db_H->Execute($sql_orders_products);
		$str = $order_no;	
		$str = $str.', ';
		$str = $str.$result_orders->fields['customers_name'];	
		$str = $str."<br>";
		while (!$result_orders_products->EOF) {
				$str = $str.$result_orders_products->fields['products_quantity']." x ".$result_orders_products->fields['products_name']."    ". floor($result_orders_products->fields['products_price'])."円";
				$sql_orders_products_attributes = "select * from orders_products_attributes  where orders_id=".$order_id."  and orders_products_id=".$result_orders_products->fields['orders_products_id'];
				$result_orders_products_attributes = $db_H->Execute($sql_orders_products_attributes);
				while (!$result_orders_products_attributes->EOF) {
					$str = $str."(".$result_orders_products_attributes->fields['products_options_values']."+".floor($result_orders_products_attributes->fields['options_values_price'])."円)";
					if($battery_flag == 0){
						if($result_orders_products_attributes->fields['products_options_values'] == '電池含む'){
							$battery_flag = 1;
						}
					}
					
					$result_orders_products_attributes->MoveNext();	
				}
				$str = $str."<br>";	
				$product_description_num = $product_description_num + $result_orders_products->fields['products_quantity'];//统计数量
				$result_orders_products->MoveNext();	
		}
		/*
		if($result_orders->fields['coupon_code']!=null && $result_orders->fields['coupon_code']!=''){
			$str = $str."Coupon[".$result_orders->fields['coupon_code']."],";
		}
		*/
	

	
		//$str = $str."小計: ".floor($sub_total)."円,";
		//$str = $str."送料: ".$shipping_fee."円,";				
		//$str = $str."合計: ".floor($order_total)."円";
		//$str = $str."Email: ".$customer_email."<br>";?

		//bof:orders_total
		$sql_orders_total = "select * from orders_total where orders_id=".$order_id;		
		$result_orders_total = $db_H->Execute($sql_orders_total);
		while (!$result_orders_total->EOF) {
			if($result_orders_total->fields['class'] == "ot_subtotal"){
				$sub_total = $result_orders_total->fields['text'];
			}else if($result_orders_total->fields['class'] == "ot_shipping"){
				$shipping_fee = $result_orders_total->fields['text'];
			}else if($result_orders_total->fields['class'] == "ot_coupon"){
				$order_coupon = $result_orders_total->fields['title'].$result_orders_total->fields['text'].", ";
			}else if($result_orders_total->fields['class'] == "ot_total"){
				$order_total = $result_orders_total->fields['text'];
			}
			$result_orders_total->MoveNext();
		}
		$str = $str.$order_coupon."小計:".$sub_total.", 送料:".$shipping_fee.", 合計:".$order_total;
		//eof:orders_total



		//$products_info = $str;		
		$products_info = $commonFunction->getSQLString($str);

		$product_description = $product_description." | ".$product_description_num." pcs | ".$commonFunction->getDeclaredPrice(floor($order_total),"yen");

		//统计订单总数
		$sql_sum_orders = "select count(*) as sum_orders from orders where customer_email = '".$customer_email."' and (order_status = 20 or order_status = 30 or order_status = 40) group by customer_email";
		$result_sum_orders = $db->Execute($sql_sum_orders);
		if(!$result_sum_orders->EOF){
			$sum_orders = $result_sum_orders->fields['sum_orders'];
		}
		if($order_status == 20){ 
			$sum_orders = $sum_orders + 1;
		}




		$sql_insert_orders = "INSERT INTO orders
		(create_time,order_no,order_site,products_info,order_status,battery_flag,tracking_flag,payment_flag,customer_name,customer_telephone,customer_address,customer_postcode,customer_email,product_description,sum_orders) 
		values 
		(NOW(),'$order_no','$order_site','$products_info',$order_status,$battery_flag,$tracking_flag,$payment_flag,'$customer_name','$customer_telephone','$customer_address','$customer_postcode','$customer_email','$product_description',$sum_orders)";
		$db->Execute($sql_insert_orders);

	}else{
		//echo "***************************************<br>没这个订单ID：".$order_H."<br>***************************************<br>";
		$errorMsg = $order_id;
	}
}else if($doFlag == "order_LJ"){
	//加新订单 Lucklaser.jp
	$sql_orders = "select * from orders where orders_id=".$order_id;
	$result_orders = $db_LJ->Execute($sql_orders);
	if(!$result_orders->EOF){
		$str = "LJ";
		$sub_total = 0; //小计
		$shipping_fee = 0; //运费
		$order_total = 0; //总计
		$order_coupon = "";//coupon
		$order_no = "LJ".$order_id;
		$order_site = "lucklaser.jp";//来自哪个网店
		$products_info = '';//订单明细
		$order_status = 10;//当前状态: 默认没付款(详见 设置页面)
		$battery_flag = 0;//是否有电池 0=没电池 1=有电池
		$tracking_flag = 0;//是否转跟单 0=没跟单 1=有跟单
		$payment_flag = 0;//付款方式 0=PayPal 1=Bank
		$pay_time = ''; //付款时间
		$customer_name = ''; //客户名字
		$customer_telephone = ''; //客户电话
		$customer_address = ''; //客户地址
		$customer_postcode = ''; //客户邮编
		$customer_email = ''; //客户email
		$product_description = "LED 手电筒 / LED Flashlight";//☆产品 数量 总价
		$product_description_num = 0;//产品 ☆数量 总价
		$product_description_price = "";//产品 数量 ☆总价
		$sum_orders = 0;//历史订单总数

		

		//$order_total = $result_orders->fields['order_total'];

		//------------满10000 免运费 ------------------------	
		/*
		if(($order_total - $shipping_fee) >= 10000){
			$shipping_fee = 0;
		}*/
		

		//$sub_total = $order_total - $shipping_fee;
		$strTmp = $result_orders->fields['payment_method'];
		$payment_flag = 0;
		$customer_name = $result_orders->fields['delivery_name'];
		$customer_telephone =  $result_orders->fields['customers_telephone'];
		
		$customer_address =  $result_orders->fields['delivery_street_address'].' '.$result_orders->fields['delivery_suburb'].', '.$result_orders->fields['delivery_city'].', '.$result_orders->fields['delivery_state'].', '.$result_orders->fields['delivery_country'];
		$customer_postcode = $result_orders->fields['delivery_postcode'];
		$customer_email = $result_orders->fields['customers_email_address'];



		$sql_orders_products = "select * from orders_products where orders_id=".$order_id;
		$result_orders_products = $db_LJ->Execute($sql_orders_products);
		$str = $order_no;	
		$str = $str.', ';
		$str = $str.$result_orders->fields['customers_name'];	
		$str = $str."<br>";
		while (!$result_orders_products->EOF) {
				$str = $str.$result_orders_products->fields['products_quantity']." x ".$result_orders_products->fields['products_name']."    ". floor($result_orders_products->fields['products_price'])."円";
				$sql_orders_products_attributes = "select * from orders_products_attributes  where orders_id=".$order_id."  and orders_products_id=".$result_orders_products->fields['orders_products_id'];
				$result_orders_products_attributes = $db_LJ->Execute($sql_orders_products_attributes);
				while (!$result_orders_products_attributes->EOF) {
					$str = $str."(".$result_orders_products_attributes->fields['products_options_values']."+".floor($result_orders_products_attributes->fields['options_values_price'])."円)";
					if($battery_flag == 0){
						if($result_orders_products_attributes->fields['products_options_values'] == '電池含む'){
							$battery_flag = 1;
						}
					}
					
					$result_orders_products_attributes->MoveNext();	
				}
				$str = $str."<br>";	
				$product_description_num = $product_description_num + $result_orders_products->fields['products_quantity'];//统计数量
				$result_orders_products->MoveNext();	
		}
		/*
		if($result_orders->fields['coupon_code']!=null && $result_orders->fields['coupon_code']!=''){
			$str = $str."Coupon[".$result_orders->fields['coupon_code']."],";
		}*/
	


		//$str = $str."小計: ".floor($sub_total)."円,";
		//$str = $str."送料: ".$shipping_fee."円,";				
		//$str = $str."合計: ".floor($order_total)."円";
		//bof:orders_total
		$sql_orders_total = "select * from orders_total where orders_id=".$order_id;		
		$result_orders_total = $db_LJ->Execute($sql_orders_total);
		while (!$result_orders_total->EOF) {
			if($result_orders_total->fields['class'] == "ot_subtotal"){
				$sub_total = $result_orders_total->fields['text'];
			}else if($result_orders_total->fields['class'] == "ot_shipping"){
				$shipping_fee = $result_orders_total->fields['text'];
			}else if($result_orders_total->fields['class'] == "ot_coupon"){
				$order_coupon = $result_orders_total->fields['title'].$result_orders_total->fields['text'].", ";
			}else if($result_orders_total->fields['class'] == "ot_total"){
				$order_total = $result_orders_total->fields['text'];
			}
			$result_orders_total->MoveNext();
		}
		$str = $str.$order_coupon."小計:".$sub_total.", 送料:".$shipping_fee.", 合計:".$order_total;
		//eof:orders_total
		//$products_info = $str;		
		$products_info = $commonFunction->getSQLString($str);

		$product_description = $product_description." | ".$product_description_num." pcs | ".$commonFunction->getDeclaredPrice(floor($order_total),"yen");


		//统计订单总数
		$sql_sum_orders = "select count(*) as sum_orders from orders where customer_email = '".$customer_email."' and (order_status = 20 or order_status = 30 or order_status = 40) group by customer_email";
		$result_sum_orders = $db->Execute($sql_sum_orders);
		if(!$result_sum_orders->EOF){
			$sum_orders = $result_sum_orders->fields['sum_orders'];
		}
		if($order_status == 20){ 
			$sum_orders = $sum_orders + 1;
		}

		$sql_insert_orders = "INSERT INTO orders
		(create_time,order_no,order_site,products_info,order_status,battery_flag,tracking_flag,payment_flag,customer_name,customer_telephone,customer_address,customer_postcode,customer_email,product_description,sum_orders) 
		values 
		(NOW(),'$order_no','$order_site','$products_info',$order_status,$battery_flag,$tracking_flag,$payment_flag,'$customer_name','$customer_telephone','$customer_address','$customer_postcode','$customer_email','$product_description',$sum_orders)";
		$db->Execute($sql_insert_orders);

	}else{
		//echo "***************************************<br>没这个订单ID：".$order_H."<br>***************************************<br>";
		$errorMsg = $order_id;
	}
}else if($doFlag == "order_LK"){
	//加新订单 Lucklaser-kr
	$sql_orders = "select * from orders where orders_id=".$order_id;
	$result_orders = $db_LK->Execute($sql_orders);
	if(!$result_orders->EOF){
		$str = "LK";
		$sub_total = 0; //小计
		$shipping_fee = 0; //运费
		$order_total = 0; //总计
		$order_coupon = "";//coupon
		$order_no = "LK".$order_id;
		$order_site = "kr.lucklaser";//来自哪个网店
		$products_info = '';//订单明细
		$order_status = 10;//当前状态: 默认没付款(详见 设置页面)
		$battery_flag = 0;//是否有电池 0=没电池 1=有电池
		$tracking_flag = 0;//是否转跟单 0=没跟单 1=有跟单
		$payment_flag = 0;//付款方式 0=PayPal 1=Bank
		$pay_time = ''; //付款时间
		$customer_name = ''; //客户名字
		$customer_telephone = ''; //客户电话
		$customer_address = ''; //客户地址
		$customer_postcode = ''; //客户邮编
		$customer_email = ''; //客户email
		$product_description = "LED 手电筒 / LED Flashlight";//☆产品 数量 总价
		$product_description_num = 0;//产品 ☆数量 总价
		$product_description_price = "";//产品 数量 ☆总价
		$sum_orders = 0;//历史订单总数


		$strTmp = $result_orders->fields['payment_method'];
		$payment_flag = 0;
		$customer_name = $result_orders->fields['delivery_name'];
		$customer_telephone =  $result_orders->fields['customers_telephone'];
		
		$customer_address =  $result_orders->fields['delivery_street_address'].' '.$result_orders->fields['delivery_suburb'].', '.$result_orders->fields['delivery_city'].', '.$result_orders->fields['delivery_state'].', '.$result_orders->fields['delivery_country'];
		$customer_postcode = $result_orders->fields['delivery_postcode'];
		$customer_email = $result_orders->fields['customers_email_address'];



		$sql_orders_products = "select * from orders_products where orders_id=".$order_id;
		$result_orders_products = $db_LK->Execute($sql_orders_products);
		$str = $order_no;	
		$str = $str.', ';
		$str = $str.$result_orders->fields['customers_name'];	
		$str = $str."<br>";
		while (!$result_orders_products->EOF) {
				$str = $str.$result_orders_products->fields['products_quantity']." x ".$result_orders_products->fields['products_name']."    ". floor($result_orders_products->fields['products_price'])."₩";
				$sql_orders_products_attributes = "select * from orders_products_attributes  where orders_id=".$order_id."  and orders_products_id=".$result_orders_products->fields['orders_products_id'];
				$result_orders_products_attributes = $db_LK->Execute($sql_orders_products_attributes);
				while (!$result_orders_products_attributes->EOF) {
					$str = $str."(".$result_orders_products_attributes->fields['products_options_values']."+".floor($result_orders_products_attributes->fields['options_values_price'])."₩)";			
					$result_orders_products_attributes->MoveNext();	
				}
				$str = $str."<br>";	
				$product_description_num = $product_description_num + $result_orders_products->fields['products_quantity'];//统计数量
				$result_orders_products->MoveNext();	
		}

		//bof:orders_total
		$sql_orders_total = "select * from orders_total where orders_id=".$order_id;		
		$result_orders_total = $db_LK->Execute($sql_orders_total);
		while (!$result_orders_total->EOF) {
			if($result_orders_total->fields['class'] == "ot_subtotal"){
				$sub_total = $result_orders_total->fields['text'];
			}else if($result_orders_total->fields['class'] == "ot_shipping"){
				$shipping_fee = $result_orders_total->fields['text'];
			}else if($result_orders_total->fields['class'] == "ot_coupon"){
				$order_coupon = $result_orders_total->fields['title'].$result_orders_total->fields['text'].", ";
			}else if($result_orders_total->fields['class'] == "ot_total"){
				$order_total = $result_orders_total->fields['text'];
			}
			$result_orders_total->MoveNext();
		}
		$str = $str.$order_coupon."Sub-Total:".$sub_total.", Shipping Fee:".$shipping_fee.", Total:".$order_total;
		//eof:orders_total
		//$products_info = $str;		
		$products_info = $commonFunction->getSQLString($str);

		$product_description = $product_description." | ".$product_description_num." pcs | ".$commonFunction->getDeclaredPrice(floor($order_total),"yen");


		//统计订单总数
		$sql_sum_orders = "select count(*) as sum_orders from orders where customer_email = '".$customer_email."' and (order_status = 20 or order_status = 30 or order_status = 40) group by customer_email";
		$result_sum_orders = $db->Execute($sql_sum_orders);
		if(!$result_sum_orders->EOF){
			$sum_orders = $result_sum_orders->fields['sum_orders'];
		}
		if($order_status == 20){ 
			$sum_orders = $sum_orders + 1;
		}

		$sql_insert_orders = "INSERT INTO orders
		(create_time,order_no,order_site,products_info,order_status,battery_flag,tracking_flag,payment_flag,customer_name,customer_telephone,customer_address,customer_postcode,customer_email,product_description,sum_orders) 
		values 
		(NOW(),'$order_no','$order_site','$products_info',$order_status,$battery_flag,$tracking_flag,$payment_flag,'$customer_name','$customer_telephone','$customer_address','$customer_postcode','$customer_email','$product_description',$sum_orders)";
		$db->Execute($sql_insert_orders);

	}else{
		$errorMsg = $order_id;
	}
}else if($doFlag == "order_L"){	
	//加新订单 Lucklaser
	//$order_id = $_POST["order_L"];
	$sql_orders = "select * from orders where orders_id=".$order_id;    
	$result_orders = $db_L->Execute($sql_orders);
	if(!$result_orders->EOF){
		$str = "L";
		$sub_total = 0; //小计
		$shipping_fee = 0; //运费-->不再hardcode
		$order_total = 0; //总计
		$order_coupon = "";//coupon
		$order_no = "L".$order_id;
		$order_site = "lucklaser";//来自哪个网店
		$products_info = '';//订单明细
		$order_status = 10;//当前状态: 默认没付款(详见 设置页面)
		$battery_flag = 0;//是否有电池 0=没电池 1=有电池
		$tracking_flag = 0;//是否转跟单 0=没跟单 1=有跟单
		$payment_flag = 0;//付款方式 0=PayPal 1=Bank 2=Alipay
		$pay_time = ''; //付款时间
		$customer_name = ''; //客户名字
		$customer_telephone = ''; //客户电话
		$customer_address = ''; //客户地址
		$customer_postcode = ''; //客户邮编
		$customer_email = ''; //客户email
		$product_description = "LED 手电筒 / LED Flashlight";//☆产品 数量 总价
		$product_description_num = 0;//产品 ☆数量 总价
		$product_description_price = "";//产品 数量 ☆总价
		$sum_orders = 0;//历史订单总数


		//$order_total = $result_orders->fields['order_total'];

		//------------满$199 免运费 订单号 527 以后开始设置------------------------	
		/*
		if(($order_total - $shipping_fee) >= 199 && $order_id > 527){
			$shipping_fee = 0;
		}*/
		

		//$sub_total = $order_total - $shipping_fee;

		$strTmp = $result_orders->fields['payment_method'];
		if($strTmp == 'Credit Card Payment Online'){
			$payment_flag = 2;
		}else{
			$payment_flag = 0;
		}

		$customer_name = $result_orders->fields['delivery_name'];
		$customer_telephone =  $result_orders->fields['customers_telephone'];
		
		$customer_address =  $result_orders->fields['delivery_street_address'].' '.$result_orders->fields['delivery_suburb'].', '.$result_orders->fields['delivery_city'].', '.$result_orders->fields['delivery_state'].', '.$result_orders->fields['delivery_country'];
		$customer_postcode = $result_orders->fields['delivery_postcode'];
		$customer_email = $result_orders->fields['customers_email_address'];

		$sql_orders_products = "select * from orders_products where orders_id=".$order_id;		
		$result_orders_products = $db_L->Execute($sql_orders_products);
		$str = $order_no;
		$str = $str.', ';
		$str = $str.$result_orders->fields['customers_name'];
		$str = $str."<br>";
		while (!$result_orders_products->EOF) {			
			$str = $str.$result_orders_products->fields['products_quantity']." x ".utf8_encode($result_orders_products->fields['products_name'])."    ". floor($result_orders_products->fields['products_price'])."$";
			$sql_orders_products_attributes = "select * from orders_products_attributes  where orders_id=".$order_id."  and orders_products_id=".$result_orders_products->fields['orders_products_id'];
			$result_orders_products_attributes = $db_L->Execute($sql_orders_products_attributes);
			while (!$result_orders_products_attributes->EOF) {
				$str = $str."(".$result_orders_products_attributes->fields['products_options_values']."+".floor($result_orders_products_attributes->fields['options_values_price'])."$)";
				/* 默认 hardcode 不带电池
				if($battery_flag == 0){
					$batterySelect = $result_orders_products_attributes->fields['products_options_values'];
					if($batterySelect == 'Battery Include' || $batterySelect == 'Include Battery'){
						$battery_flag = 1;
					}
				}*/				
				$result_orders_products_attributes->MoveNext();	
			}
			$str = $str."<br>";	
			$product_description_num = $product_description_num + $result_orders_products->fields['products_quantity'];//统计数量
			$result_orders_products->MoveNext();
		}
		/*
		if($result_orders->fields['coupon_code']!=null && $result_orders->fields['coupon_code']!=''){
			$str = $str."Coupon[".$result_orders->fields['coupon_code']."],";
		}*/
		/*
		$str = $str."Sub-Total: $".floor($sub_total).",";
		$str = $str."Shipping Fee: $".$shipping_fee.",";				
		$str = $str."Total: $".floor($order_total);
		*/
		//bof:orders_total
		$sql_orders_total = "select * from orders_total where orders_id=".$order_id;		
		$result_orders_total = $db_L->Execute($sql_orders_total);
		while (!$result_orders_total->EOF) {
			if($result_orders_total->fields['class'] == "ot_subtotal"){
				$sub_total = $result_orders_total->fields['text'];
			}else if($result_orders_total->fields['class'] == "ot_shipping"){
				$shipping_fee = $result_orders_total->fields['text'];
			}else if($result_orders_total->fields['class'] == "ot_coupon"){
				$order_coupon = $result_orders_total->fields['title'].$result_orders_total->fields['text'].", ";
			}else if($result_orders_total->fields['class'] == "ot_total"){
				$order_total = $result_orders_total->fields['text'];
			}
			$result_orders_total->MoveNext();
		}
		$str = $str.$order_coupon."Sub-Total:".$sub_total.", Shipping Fee:".$shipping_fee.", Total:".$order_total;
		//eof:orders_total
		

		//$products_info = $str;
		$products_info = $commonFunction->getSQLString($str);

		$product_description = $product_description." | ".$product_description_num." pcs | ".$commonFunction->getDeclaredPrice(floor($order_total),"dollar");

		//特殊字符处理
		$products_info = iconv('iso-8859-1','utf-8',$products_info);
		$customer_name = iconv('iso-8859-1','utf-8',$customer_name);
		$customer_address = iconv('iso-8859-1','utf-8',$customer_address);
		$products_info  = addslashes($products_info);
		$customer_name  = addslashes($customer_name);
		$customer_address  = addslashes($customer_address);


		//统计订单总数
		$sql_sum_orders = "select count(*) as sum_orders from orders where customer_email = '".$customer_email."' and (order_status = 20 or order_status = 30 or order_status = 40) group by customer_email";
		$result_sum_orders = $db->Execute($sql_sum_orders);
		if(!$result_sum_orders->EOF){
			$sum_orders = $result_sum_orders->fields['sum_orders'];
		}
		if($order_status == 20){ 
			$sum_orders = $sum_orders + 1;
		}

		



		$sql_insert_orders = "INSERT INTO orders
		(create_time,order_no,order_site,products_info,order_status,battery_flag,tracking_flag,payment_flag,customer_name,customer_telephone,customer_address,customer_postcode,customer_email,product_description,sum_orders) 
		values 
		(NOW(),'$order_no','$order_site','$products_info',$order_status,$battery_flag,$tracking_flag,$payment_flag,'$customer_name','$customer_telephone','$customer_address','$customer_postcode','$customer_email','$product_description',$sum_orders)";
		

		$db->Execute($sql_insert_orders);

	}else{
		//echo "***************************************<br>没这个订单ID：".$order_L."<br>***************************************<br>";
		$errorMsg = $order_id;
	}
}else if($doFlag == "order_C"){	
	//加新订单 Civillaser
	//$order_id = $_POST["order_C"];
	$sql_orders = "select * from orders where orders_id=".$order_id;
	$result_orders = $db_C->Execute($sql_orders);
	if(!$result_orders->EOF){
		$str = "C";
		$sub_total = 0; //小计
		$shipping_fee = 0; //运费
		$order_total = 0; //总计
		$order_coupon = "";//coupon
		$order_no = "C".$order_id;
		$order_site = "civillaser";//来自哪个网店
		$products_info = '';//订单明细
		$order_status = 20;//当前状态: 默认没付款(详见 设置页面)
		$battery_flag = 0;//是否有电池 0=没电池 1=有电池
		$tracking_flag = 0;//是否转跟单 0=没跟单 1=有跟单
		$payment_flag = 0;//付款方式 0=PayPal 1=Bank
		$pay_time = ''; //付款时间
		$customer_name = ''; //客户名字
		$customer_telephone = ''; //客户电话
		$customer_address = ''; //客户地址
		$customer_postcode = ''; //客户邮编
		$customer_email = ''; //客户email
		$product_description = "激光模组 / Laser Module";//☆产品 数量 总价
		$product_description_num = 0;//产品 ☆数量 总价
		$product_description_price = "";//产品 数量 ☆总价
		$sum_orders = 0;//历史订单总数


		//$order_total = $result_orders->fields['order_total'];
		//$sub_total = $order_total - $shipping_fee;
		$customer_name = $result_orders->fields['delivery_name'];
		$customer_telephone =  $result_orders->fields['customers_telephone'];
		
		$customer_address =  $result_orders->fields['delivery_street_address'].' '.$result_orders->fields['delivery_suburb'].', '.$result_orders->fields['delivery_city'].', '.$result_orders->fields['delivery_state'].', '.$result_orders->fields['delivery_country'];
		$customer_postcode = $result_orders->fields['delivery_postcode'];
		$customer_email = $result_orders->fields['customers_email_address'];

		$sql_orders_products = "select * from orders_products where orders_id=".$order_id;
		$result_orders_products = $db_C->Execute($sql_orders_products);
		$str = $order_no;
		$str = $str.', ';
		$str = $str.$result_orders->fields['customers_name'];
		$str = $str."<br>";
		while (!$result_orders_products->EOF) {
			$str = $str.$result_orders_products->fields['products_quantity']." x ".$result_orders_products->fields['products_name']."    ". floor($result_orders_products->fields['products_price'])."$";
			$sql_orders_products_attributes = "select * from orders_products_attributes  where orders_id=".$order_id."  and orders_products_id=".$result_orders_products->fields['orders_products_id'];
			$result_orders_products_attributes = $db_C->Execute($sql_orders_products_attributes);
			while (!$result_orders_products_attributes->EOF) {
				$str = $str."(".$result_orders_products_attributes->fields['products_options_values']."+".floor($result_orders_products_attributes->fields['options_values_price'])."$)";
				if($battery_flag == 0){
					if($result_orders_products_attributes->fields['products_options_values'] == 'Battery Include'){
						$battery_flag = 1;
					}
				}
				$result_orders_products_attributes->MoveNext();	
			}
			$str = $str."<br>";	
			$product_description_num = $product_description_num + $result_orders_products->fields['products_quantity'];//统计数量
			$result_orders_products->MoveNext();
		}
		/*
		if($result_orders->fields['coupon_code']!=null && $result_orders->fields['coupon_code']!=''){
			$str = $str."Coupon[".$result_orders->fields['coupon_code']."],";
		}
		$str = $str."Sub-Total: $".floor($sub_total).",";
		$str = $str."Shipping Fee: $".$shipping_fee.",";				
		$str = $str."Total: $".floor($order_total);
		*/
		//bof:orders_total
		$sql_orders_total = "select * from orders_total where orders_id=".$order_id;		
		$result_orders_total = $db_C->Execute($sql_orders_total);
		while (!$result_orders_total->EOF) {
			if($result_orders_total->fields['class'] == "ot_subtotal"){
				$sub_total = $result_orders_total->fields['text'];
			}else if($result_orders_total->fields['class'] == "ot_shipping"){
				$shipping_fee = $result_orders_total->fields['text'];
			}else if($result_orders_total->fields['class'] == "ot_coupon"){
				$order_coupon = $result_orders_total->fields['title'].$result_orders_total->fields['text'].", ";
			}else if($result_orders_total->fields['class'] == "ot_total"){
				$order_total = $result_orders_total->fields['text'];
			}
			$result_orders_total->MoveNext();
		}
		$str = $str.$order_coupon."Sub-Total:".$sub_total.", Shipping Fee:".$shipping_fee.", Total:".$order_total;
		//eof:orders_total
		$products_info = $commonFunction->getSQLString($str);
		
		$product_description = $product_description." | ".$product_description_num." pcs | ".$commonFunction->getDeclaredPrice(floor($order_total),"dollar");


		//特殊字符处理		
		$products_info  = addslashes($products_info);
		$customer_name  = addslashes($customer_name);
		$customer_address  = addslashes($customer_address);


		//统计订单总数
		$sql_sum_orders = "select count(*) as sum_orders from orders where customer_email = '".$customer_email."' and (order_status = 20 or order_status = 30 or order_status = 40) group by customer_email";
		$result_sum_orders = $db->Execute($sql_sum_orders);
		if(!$result_sum_orders->EOF){
			$sum_orders = $result_sum_orders->fields['sum_orders'];
		}
		if($order_status == 20){ 
			$sum_orders = $sum_orders + 1;
		}


		$sql_insert_orders = "INSERT INTO orders
		(create_time,order_no,order_site,products_info,order_status,battery_flag,tracking_flag,payment_flag,customer_name,customer_telephone,customer_address,customer_postcode,customer_email,product_description,sum_orders) 
		values 
		(NOW(),'$order_no','$order_site','$products_info',$order_status,$battery_flag,$tracking_flag,$payment_flag,'$customer_name','$customer_telephone','$customer_address','$customer_postcode','$customer_email','$product_description',$sum_orders)";
		$db->Execute($sql_insert_orders);
	}else{
		//echo "***************************************<br>没这个订单ID：".$order_L."<br>***************************************<br>";
		$errorMsg = $order_id;
	}
}else if($doFlag == "order_CJ"){
	//加新订单 Civillaser-jp
	//$order_id = $_POST["order_CJ"];
	$sql_orders = "select * from orders where orders_id=".$order_id;
	$result_orders = $db_CJ->Execute($sql_orders);
	if(!$result_orders->EOF){
		$str = "CJ";
		$sub_total = 0; //小计
		$shipping_fee = 0; //运费
		$order_total = 0; //总计
		$order_coupon = "";//coupon
		$order_no = "CJ".$order_id;
		$order_site = "jp.civillaser";//来自哪个网店
		$products_info = '';//订单明细
		$order_status = 20;//当前状态: 默认已付款(详见 设置页面)
		$battery_flag = 0;//是否有电池 0=没电池 1=有电池
		$tracking_flag = 0;//是否转跟单 0=没跟单 1=有跟单
		$payment_flag = 0;//付款方式 0=PayPal 1=Bank
		$pay_time = ''; //付款时间
		$customer_name = ''; //客户名字
		$customer_telephone = ''; //客户电话
		$customer_address = ''; //客户地址
		$customer_postcode = ''; //客户邮编
		$customer_email = ''; //客户email
		$product_description = "激光模组 / Laser Module";//☆产品 数量 价格
		$product_description_num = 0;//产品 ☆数量 价格
		$product_description_price = "";//产品 数量 ☆价格
		$sum_orders = 0;//历史订单总数

		//$order_total = $result_orders->fields['order_total'];
		//$sub_total = $order_total - $shipping_fee;

		$customer_name = $result_orders->fields['delivery_name'];
		$customer_telephone =  $result_orders->fields['customers_telephone'];
		$customer_address =  $result_orders->fields['delivery_street_address'].' '.$result_orders->fields['delivery_suburb'].', '.$result_orders->fields['delivery_city'].', '.$result_orders->fields['delivery_state'].', '.$result_orders->fields['delivery_country'];
		$customer_postcode = $result_orders->fields['delivery_postcode'];
		$customer_email = $result_orders->fields['customers_email_address'];



		$sql_orders_products = "select * from orders_products where orders_id=".$order_id;
		$result_orders_products = $db_CJ->Execute($sql_orders_products);
		$str = $order_no;	
		$str = $str.', ';
		$str = $str.$result_orders->fields['customers_name'];	
		$str = $str."<br>";
		while (!$result_orders_products->EOF) {
				$str = $str.$result_orders_products->fields['products_quantity']." x ".$result_orders_products->fields['products_name']."    ". floor($result_orders_products->fields['products_price'])."円";
				$sql_orders_products_attributes = "select * from orders_products_attributes  where orders_id=".$order_id."  and orders_products_id=".$result_orders_products->fields['orders_products_id'];
				$result_orders_products_attributes = $db_CJ->Execute($sql_orders_products_attributes);
				while (!$result_orders_products_attributes->EOF) {
					$str = $str."(".$result_orders_products_attributes->fields['products_options_values']."+".floor($result_orders_products_attributes->fields['options_values_price'])."円)";
					if($battery_flag == 0){
						if($result_orders_products_attributes->fields['products_options_values'] == '電池含む'){
							$battery_flag = 1;
						}
					}
					
					$result_orders_products_attributes->MoveNext();	
				}
				$str = $str."<br>";	
				$product_description_num = $product_description_num + $result_orders_products->fields['products_quantity'];//统计数量
				$result_orders_products->MoveNext();	
		}
		/*
		if($result_orders->fields['coupon_code']!=null && $result_orders->fields['coupon_code']!=''){
			$str = $str."Coupon[".$result_orders->fields['coupon_code']."],";
		}
		$str = $str."小計: ".floor($sub_total)."円,";
		$str = $str."送料: ".$shipping_fee."円,";				
		$str = $str."合計: ".floor($order_total)."円";
		//$str = $str."Email: ".$customer_email."<br>";
		*/
		//bof:orders_total
		$sql_orders_total = "select * from orders_total where orders_id=".$order_id;		
		$result_orders_total = $db_CJ->Execute($sql_orders_total);
		while (!$result_orders_total->EOF) {
			if($result_orders_total->fields['class'] == "ot_subtotal"){
				$sub_total = $result_orders_total->fields['text'];
			}else if($result_orders_total->fields['class'] == "ot_shipping"){
				$shipping_fee = $result_orders_total->fields['text'];
			}else if($result_orders_total->fields['class'] == "ot_coupon"){
				$order_coupon = $result_orders_total->fields['title'].$result_orders_total->fields['text'].", ";
			}else if($result_orders_total->fields['class'] == "ot_total"){
				$order_total = $result_orders_total->fields['text'];
			}
			$result_orders_total->MoveNext();
		}
		$str = $str.$order_coupon."小計:".$sub_total.", 送料:".$shipping_fee.", 合計:".$order_total;
		//eof:orders_total
		//$products_info = $str;	
		$products_info = $commonFunction->getSQLString($str);
		
		$product_description = $product_description." | ".$product_description_num." pcs | ".$commonFunction->getDeclaredPrice(floor($order_total),"yen");

		//统计订单总数
		$sql_sum_orders = "select count(*) as sum_orders from orders where customer_email = '".$customer_email."' and (order_status = 20 or order_status = 30 or order_status = 40) group by customer_email";
		$result_sum_orders = $db->Execute($sql_sum_orders);
		if(!$result_sum_orders->EOF){
			$sum_orders = $result_sum_orders->fields['sum_orders'];
		}
		if($order_status == 20){ 
			$sum_orders = $sum_orders + 1;
		}


		$sql_insert_orders = "INSERT INTO orders
		(create_time,order_no,order_site,products_info,order_status,battery_flag,tracking_flag,payment_flag,customer_name,customer_telephone,customer_address,customer_postcode,customer_email,product_description,sum_orders) 
		values 
		(NOW(),'$order_no','$order_site','$products_info',$order_status,$battery_flag,$tracking_flag,$payment_flag,'$customer_name','$customer_telephone','$customer_address','$customer_postcode','$customer_email','$product_description',$sum_orders)";
		$db->Execute($sql_insert_orders);

	}else{
		//echo "***************************************<br>没这个订单ID：".$order_CJ."<br>***************************************<br>";
		$errorMsg = $order_id;
	}
}else if($doFlag == "order_LD"){ //不再更新 2015-2-25
	//加新订单 Laser-Door
	//$order_id = $_POST["order_LD"];
	$sql_orders = "select * from orders where orders_id=".$order_id;
	$result_orders = $db_LD->Execute($sql_orders);
	if(!$result_orders->EOF){
		$str = "LD";
		$sub_total = 0; //小计
		$shipping_fee = 900; //运费
		$order_total = 0; //总计
		$order_no = "LD".$order_id;
		$order_site = "laser-door";//来自哪个网店
		$products_info = '';//订单明细
		$order_status = 20;//当前状态: 默认已付款(详见 设置页面)
		$battery_flag = 0;//是否有电池 0=没电池 1=有电池
		$tracking_flag = 0;//是否转跟单 0=没跟单 1=有跟单
		$payment_flag = 0;//付款方式 0=PayPal 1=Bank
		$pay_time = ''; //付款时间
		$customer_name = ''; //客户名字
		$customer_telephone = ''; //客户电话
		$customer_address = ''; //客户地址
		$customer_postcode = ''; //客户邮编
		$customer_email = ''; //客户email
		$product_description = "激光模组 / Laser Module";//☆产品 数量 价格
		$product_description_num = 0;//产品 ☆数量 价格
		$product_description_price = "";//产品 数量 ☆价格

		$order_total = $result_orders->fields['order_total'];
		$sub_total = $order_total - $shipping_fee;

		$customer_name = $result_orders->fields['delivery_name'];
		$customer_telephone =  $result_orders->fields['customers_telephone'];
		
		$customer_address =  $result_orders->fields['delivery_street_address'].' '.$result_orders->fields['delivery_suburb'].', '.$result_orders->fields['delivery_city'].', '.$result_orders->fields['delivery_state'].', '.$result_orders->fields['delivery_country'];
		$customer_postcode = $result_orders->fields['delivery_postcode'];
		$customer_email = $result_orders->fields['customers_email_address'];



		$sql_orders_products = "select * from orders_products where orders_id=".$order_id;
		$result_orders_products = $db_LD->Execute($sql_orders_products);
		$str = $order_no;	
		$str = $str.', ';
		$str = $str.$result_orders->fields['customers_name'];	
		$str = $str."<br>";
		while (!$result_orders_products->EOF) {
				$str = $str.$result_orders_products->fields['products_quantity']." x ".$result_orders_products->fields['products_name']."    ". floor($result_orders_products->fields['products_price'])."円";
				$sql_orders_products_attributes = "select * from orders_products_attributes  where orders_id=".$order_id."  and orders_products_id=".$result_orders_products->fields['orders_products_id'];
				$result_orders_products_attributes = $db_LD->Execute($sql_orders_products_attributes);
				while (!$result_orders_products_attributes->EOF) {
					$str = $str."(".$result_orders_products_attributes->fields['products_options_values']."+".floor($result_orders_products_attributes->fields['options_values_price'])."円)";
					if($battery_flag == 0){
						if($result_orders_products_attributes->fields['products_options_values'] == '電池含む'){
							$battery_flag = 1;
						}
					}
					
					$result_orders_products_attributes->MoveNext();	
				}
				$str = $str."<br>";
				$product_description_num = $product_description_num + $result_orders_products->fields['products_quantity'];//统计数量
				$result_orders_products->MoveNext();	
		}
		if($result_orders->fields['coupon_code']!=null && $result_orders->fields['coupon_code']!=''){
			$str = $str."Coupon[".$result_orders->fields['coupon_code']."],";
		}
		$str = $str."小計: ".floor($sub_total)."円,";
		$str = $str."送料: ".$shipping_fee."円,";				
		$str = $str."合計: ".floor($order_total)."円";
		//$str = $str."Email: ".$customer_email."<br>";
		//$products_info = $str;	
		$products_info = $commonFunction->getSQLString($str);
		$product_description = $product_description." | ".$product_description_num." pcs | ".$commonFunction->getDeclaredPrice(floor($order_total),"yen");

		$sql_insert_orders = "INSERT INTO orders
		(create_time,order_no,order_site,products_info,order_status,battery_flag,tracking_flag,payment_flag,customer_name,customer_telephone,customer_address,customer_postcode,customer_email,product_description) 
		values 
		(NOW(),'$order_no','$order_site','$products_info',$order_status,$battery_flag,$tracking_flag,$payment_flag,'$customer_name','$customer_telephone','$customer_address','$customer_postcode','$customer_email','$product_description')";
		$db->Execute($sql_insert_orders);

	}else{
		//echo "***************************************<br>没这个订单ID：".$order_LD."<br>***************************************<br>";
		$errorMsg = $order_id;
	}
}else if($doFlag == "order_K"){ //不再更新 2015-2-25
	//加新订单 Kennbikyou
	//$order_id = $_POST["order_K"];
	$sql_orders = "select * from orders where orders_id=".$order_id;
	$result_orders = $db_K->Execute($sql_orders);
	if(!$result_orders->EOF){
		$str = "K";
		$sub_total = 0; //小计
		$shipping_fee = 900; //运费
		$order_total = 0; //总计
		$order_no = "K".$order_id;
		$order_site = "kennbikyou";//来自哪个网店
		$products_info = '';//订单明细
		$order_status = 20;//当前状态: 默认已付款(详见 设置页面)
		$battery_flag = 0;//是否有电池 0=没电池 1=有电池
		$tracking_flag = 0;//是否转跟单 0=没跟单 1=有跟单
		$payment_flag = 0;//付款方式 0=PayPal 1=Bank
		$product_description = "数码显微镜 / Digital Microscope";
		$pay_time = ''; //付款时间
		$customer_name = ''; //客户名字
		$customer_telephone = ''; //客户电话
		$customer_address = ''; //客户地址
		$customer_postcode = ''; //客户邮编
		$customer_email = ''; //客户email

		$order_total = $result_orders->fields['order_total'];
		$sub_total = $order_total - $shipping_fee;

		$customer_name = $result_orders->fields['delivery_name'];
		$customer_telephone =  $result_orders->fields['customers_telephone'];
		
		$customer_address =  $result_orders->fields['delivery_street_address'].' '.$result_orders->fields['delivery_suburb'].', '.$result_orders->fields['delivery_city'].', '.$result_orders->fields['delivery_state'].', '.$result_orders->fields['delivery_country'];
		$customer_postcode = $result_orders->fields['delivery_postcode'];
		$customer_email = $result_orders->fields['customers_email_address'];



		$sql_orders_products = "select * from orders_products where orders_id=".$order_id;
		$result_orders_products = $db_K->Execute($sql_orders_products);
		$str = $order_no;	
		$str = $str.', ';
		$str = $str.$result_orders->fields['customers_name'];	
		$str = $str."<br>";
		while (!$result_orders_products->EOF) {
				$str = $str.$result_orders_products->fields['products_quantity']." x ".$result_orders_products->fields['products_name']."    ". floor($result_orders_products->fields['products_price'])."円";
				$sql_orders_products_attributes = "select * from orders_products_attributes  where orders_id=".$order_id."  and orders_products_id=".$result_orders_products->fields['orders_products_id'];
				$result_orders_products_attributes = $db_K->Execute($sql_orders_products_attributes);
				while (!$result_orders_products_attributes->EOF) {
					$str = $str."(".$result_orders_products_attributes->fields['products_options_values']."+".floor($result_orders_products_attributes->fields['options_values_price'])."円)";
					if($battery_flag == 0){
						if($result_orders_products_attributes->fields['products_options_values'] == '電池含む'){
							$battery_flag = 1;
						}
					}
					
					$result_orders_products_attributes->MoveNext();	
				}
				$str = $str."<br>";				
				$result_orders_products->MoveNext();	
		}
		if($result_orders->fields['coupon_code']!=null && $result_orders->fields['coupon_code']!=''){
			$str = $str."Coupon[".$result_orders->fields['coupon_code']."],";
		}
		$str = $str."小計: ".floor($sub_total)."円,";
		$str = $str."送料: ".$shipping_fee."円,";				
		$str = $str."合計: ".floor($order_total)."円";
		//$products_info = $str;		
		$products_info = $commonFunction->getSQLString($str);
		$sql_insert_orders = "INSERT INTO orders
		(create_time,order_no,order_site,products_info,order_status,battery_flag,tracking_flag,payment_flag,customer_name,customer_telephone,customer_address,customer_postcode,customer_email,product_description) 
		values 
		(NOW(),'$order_no','$order_site','$products_info',$order_status,$battery_flag,$tracking_flag,$payment_flag,'$customer_name','$customer_telephone','$customer_address','$customer_postcode','$customer_email','$product_description')";
		$db->Execute($sql_insert_orders);

	}else{
		//echo "***************************************<br>没这个订单ID：".$order_K."<br>***************************************<br>";
		$errorMsg = $order_id;
	}
}else if($doFlag == "order_D"){	
	//加新订单 DMicroscope
	//$order_id = $_POST["order_D"];
	$sql_orders = "select * from orders where orders_id=".$order_id;
	$result_orders = $db_D->Execute($sql_orders);
	if(!$result_orders->EOF){
		$str = "D";
		$sub_total = 0; //小计
		$shipping_fee = 18; //运费
		$order_total = 0; //总计
		$order_no = "D".$order_id;
		$order_site = "dmicroscope";//来自哪个网店
		$products_info = '';//订单明细
		$order_status = 20;//当前状态: 默认没付款(详见 设置页面)
		$battery_flag = 0;//是否有电池 0=没电池 1=有电池
		$tracking_flag = 0;//是否转跟单 0=没跟单 1=有跟单
		$payment_flag = 0;//付款方式 0=PayPal 1=Bank
		$pay_time = ''; //付款时间
		$product_description = "数码显微镜 / Digital Microscope";
		$customer_name = ''; //客户名字
		$customer_telephone = ''; //客户电话
		$customer_address = ''; //客户地址
		$customer_postcode = ''; //客户邮编
		$customer_email = ''; //客户email

		$order_total = $result_orders->fields['order_total'];
		$sub_total = $order_total - $shipping_fee;
		$customer_name = $result_orders->fields['delivery_name'];
		$customer_telephone =  $result_orders->fields['customers_telephone'];
		
		$customer_address =  $result_orders->fields['delivery_street_address'].' '.$result_orders->fields['delivery_suburb'].', '.$result_orders->fields['delivery_city'].', '.$result_orders->fields['delivery_state'].', '.$result_orders->fields['delivery_country'];
		$customer_postcode = $result_orders->fields['delivery_postcode'];
		$customer_email = $result_orders->fields['customers_email_address'];

		$sql_orders_products = "select * from orders_products where orders_id=".$order_id;
		$result_orders_products = $db_D->Execute($sql_orders_products);
		$str = $order_no;
		$str = $str.', ';
		$str = $str.$result_orders->fields['customers_name'];
		$str = $str."<br>";
		while (!$result_orders_products->EOF) {
			$str = $str.$result_orders_products->fields['products_quantity']." x ".$result_orders_products->fields['products_name']."    ". floor($result_orders_products->fields['products_price'])."$";
			$sql_orders_products_attributes = "select * from orders_products_attributes  where orders_id=".$order_id."  and orders_products_id=".$result_orders_products->fields['orders_products_id'];
			$result_orders_products_attributes = $db_D->Execute($sql_orders_products_attributes);
			while (!$result_orders_products_attributes->EOF) {
				$str = $str."(".$result_orders_products_attributes->fields['products_options_values']."+".floor($result_orders_products_attributes->fields['options_values_price'])."$)";
				if($battery_flag == 0){
					if($result_orders_products_attributes->fields['products_options_values'] == 'Battery Include'){
						$battery_flag = 1;
					}
				}
				$result_orders_products_attributes->MoveNext();	
			}
			$str = $str."<br>";	
			$result_orders_products->MoveNext();
		}
		if($result_orders->fields['coupon_code']!=null && $result_orders->fields['coupon_code']!=''){
			$str = $str."Coupon[".$result_orders->fields['coupon_code']."],";
		}
		$str = $str."Sub-Total: $".floor($sub_total).",";
		$str = $str."Shipping Fee: $".$shipping_fee.",";				
		$str = $str."Total: $".floor($order_total);
		//$products_info = $str;
		$products_info = $commonFunction->getSQLString($str);
		$sql_insert_orders = "INSERT INTO orders
		(create_time,order_no,order_site,products_info,order_status,battery_flag,tracking_flag,payment_flag,customer_name,customer_telephone,customer_address,customer_postcode,customer_email,product_description) 
		values 
		(NOW(),'$order_no','$order_site','$products_info',$order_status,$battery_flag,$tracking_flag,$payment_flag,'$customer_name','$customer_telephone','$customer_address','$customer_postcode','$customer_email','$product_description')";
		$db->Execute($sql_insert_orders);
	}else{
		//echo "***************************************<br>没这个订单ID：".$order_D."<br>***************************************<br>";
		$errorMsg = $order_id;
	}
}else if($doFlag == "order_S"){	
	//加新订单 Spycamhidden
	//$order_id = $_POST["order_S"];
	$sql_orders = "select * from orders where orders_id=".$order_id;
	$result_orders = $db_S->Execute($sql_orders);
	if(!$result_orders->EOF){
		$str = "S";
		$sub_total = 0; //小计
		$shipping_fee = 18; //运费
		$order_total = 0; //总计
		$order_no = "S".$order_id;
		$order_site = "spycamhidden";//来自哪个网店
		$products_info = '';//订单明细
		$order_status = 20;//当前状态: 默认没付款(详见 设置页面)
		$battery_flag = 0;//是否有电池 0=没电池 1=有电池
		$tracking_flag = 0;//是否转跟单 0=没跟单 1=有跟单
		$payment_flag = 0;//付款方式 0=PayPal 1=Bank
		$pay_time = ''; //付款时间
		$product_description = " 数码相机 / Digital camera";
		$customer_name = ''; //客户名字
		$customer_telephone = ''; //客户电话
		$customer_address = ''; //客户地址
		$customer_postcode = ''; //客户邮编
		$customer_email = ''; //客户email

		$order_total = $result_orders->fields['order_total'];
		$sub_total = $order_total - $shipping_fee;
		$customer_name = $result_orders->fields['delivery_name'];
		$customer_telephone =  $result_orders->fields['customers_telephone'];
		
		$customer_address =  $result_orders->fields['delivery_street_address'].' '.$result_orders->fields['delivery_suburb'].', '.$result_orders->fields['delivery_city'].', '.$result_orders->fields['delivery_state'].', '.$result_orders->fields['delivery_country'];
		$customer_postcode = $result_orders->fields['delivery_postcode'];
		$customer_email = $result_orders->fields['customers_email_address'];

		$sql_orders_products = "select * from orders_products where orders_id=".$order_id;
		$result_orders_products = $db_S->Execute($sql_orders_products);
		$str = $order_no;
		$str = $str.', ';
		$str = $str.$result_orders->fields['customers_name'];
		$str = $str."<br>";
		while (!$result_orders_products->EOF) {
			$str = $str.$result_orders_products->fields['products_quantity']." x ".$result_orders_products->fields['products_name']."    ". floor($result_orders_products->fields['products_price'])."$";
			$sql_orders_products_attributes = "select * from orders_products_attributes  where orders_id=".$order_id."  and orders_products_id=".$result_orders_products->fields['orders_products_id'];
			$result_orders_products_attributes = $db_S->Execute($sql_orders_products_attributes);
			while (!$result_orders_products_attributes->EOF) {
				$str = $str."(".$result_orders_products_attributes->fields['products_options_values']."+".floor($result_orders_products_attributes->fields['options_values_price'])."$)";
				if($battery_flag == 0){
					if($result_orders_products_attributes->fields['products_options_values'] == 'Battery Include'){
						$battery_flag = 1;
					}
				}
				$result_orders_products_attributes->MoveNext();	
			}
			$str = $str."<br>";	
			$result_orders_products->MoveNext();
		}
		if($result_orders->fields['coupon_code']!=null && $result_orders->fields['coupon_code']!=''){
			$str = $str."Coupon[".$result_orders->fields['coupon_code']."],";
		}
		$str = $str."Sub-Total: $".floor($sub_total).",";
		$str = $str."Shipping Fee: $".$shipping_fee.",";				
		$str = $str."Total: $".floor($order_total);
		//$products_info = $str;
		$products_info = $commonFunction->getSQLString($str);
		$sql_insert_orders = "INSERT INTO orders
		(create_time,order_no,order_site,products_info,order_status,battery_flag,tracking_flag,payment_flag,customer_name,customer_telephone,customer_address,customer_postcode,customer_email,product_description) 
		values 
		(NOW(),'$order_no','$order_site','$products_info',$order_status,$battery_flag,$tracking_flag,$payment_flag,'$customer_name','$customer_telephone','$customer_address','$customer_postcode','$customer_email','$product_description')";
		$db->Execute($sql_insert_orders);
	}else{
		//echo "***************************************<br>没这个订单ID：".$order_S."<br>***************************************<br>";
		$errorMsg = $order_id;
	}
}else if($doFlag == "order_CA"){	
	//加新订单 Canetalaser
	$sql_orders = "select * from orders where orders_id=".$order_id;    
	$result_orders = $db_CA->Execute($sql_orders);
	if(!$result_orders->EOF){
		$str = "CA";
		$sub_total = 0; //小计
		$shipping_fee = 0; //运费
		$order_total = 0; //总计
		$order_coupon = "";//coupon
		$order_no = "CA".$order_id;
		$order_site = "canetalaser";//来自哪个网店
		$products_info = '';//订单明细
		$order_status = 10;//当前状态: 默认没付款(详见 设置页面)
		$battery_flag = 0;//是否有电池 0=没电池 1=有电池
		$tracking_flag = 0;//是否转跟单 0=没跟单 1=有跟单
		$payment_flag = 0;//付款方式 0=PayPal 1=Bank
		$pay_time = ''; //付款时间
		$customer_name = ''; //客户名字
		$customer_telephone = ''; //客户电话
		$customer_address = ''; //客户地址
		$customer_postcode = ''; //客户邮编
		$customer_email = ''; //客户email
		$product_description = "LED 手电筒 / LED Flashlight";//☆产品 数量 价格
		$product_description_num = 0;//产品 ☆数量 价格
		$product_description_price = "";//产品 数量 ☆价格
		$sum_orders = 0;//历史订单总数

		//$order_total = $result_orders->fields['order_total'];
		//$sub_total = $order_total - $shipping_fee;
		$customer_name = $result_orders->fields['delivery_name'];
		$customer_telephone =  $result_orders->fields['customers_telephone'];
		
		$customer_address =  $result_orders->fields['delivery_street_address'].' '.$result_orders->fields['delivery_suburb'].', '.$result_orders->fields['delivery_city'].', '.$result_orders->fields['delivery_state'].', '.$result_orders->fields['delivery_country'];
		$customer_postcode = $result_orders->fields['delivery_postcode'];
		$customer_email = $result_orders->fields['customers_email_address'];

		$sql_orders_products = "select * from orders_products where orders_id=".$order_id;		
		$result_orders_products = $db_CA->Execute($sql_orders_products);
		$str = $order_no;
		$str = $str.', ';
		$str = $str.$result_orders->fields['customers_name'];
		$str = $str."<br>";
		while (!$result_orders_products->EOF) {			
			$str = $str.$result_orders_products->fields['products_quantity']." x ".utf8_encode($result_orders_products->fields['products_name'])."    ". floor($result_orders_products->fields['products_price'])."$";
			$sql_orders_products_attributes = "select * from orders_products_attributes  where orders_id=".$order_id."  and orders_products_id=".$result_orders_products->fields['orders_products_id'];
			$result_orders_products_attributes = $db_CA->Execute($sql_orders_products_attributes);
			while (!$result_orders_products_attributes->EOF) {
				$str = $str."(".$result_orders_products_attributes->fields['products_options_values']."+".floor($result_orders_products_attributes->fields['options_values_price'])."$)";
				/*
				if($battery_flag == 0){
					$batterySelect = $result_orders_products_attributes->fields['products_options_values'];
					if($batterySelect == 'Battery Include' || $batterySelect == 'Include Battery'){
						$battery_flag = 1;
					}
				}
				*/
				$result_orders_products_attributes->MoveNext();	
			}
			$str = $str."<br>";	
			$product_description_num = $product_description_num + $result_orders_products->fields['products_quantity'];//统计数量
			$result_orders_products->MoveNext();
		}
		/*
		if($result_orders->fields['coupon_code']!=null && $result_orders->fields['coupon_code']!=''){
			$str = $str."Coupon[".$result_orders->fields['coupon_code']."],";
		}
		$str = $str."Sub-Total: $".floor($sub_total).",";
		$str = $str."Shipping Fee: $".$shipping_fee.",";				
		$str = $str."Total: $".floor($order_total);
		*/
		//bof:orders_total
		$sql_orders_total = "select * from orders_total where orders_id=".$order_id;		
		$result_orders_total = $db_CA->Execute($sql_orders_total);
		while (!$result_orders_total->EOF) {
			if($result_orders_total->fields['class'] == "ot_subtotal"){
				$sub_total = $result_orders_total->fields['text'];
			}else if($result_orders_total->fields['class'] == "ot_shipping"){
				$shipping_fee = $result_orders_total->fields['text'];
			}else if($result_orders_total->fields['class'] == "ot_coupon"){
				$order_coupon = $result_orders_total->fields['title'].$result_orders_total->fields['text'].", ";
			}else if($result_orders_total->fields['class'] == "ot_total"){
				$order_total = $result_orders_total->fields['text'];
			}
			$result_orders_total->MoveNext();
		}
		$str = $str.$order_coupon."Sub-Total:".$sub_total.", Shipping Fee:".$shipping_fee.", Total:".$order_total;
		//eof:orders_total
		//$products_info = $str;
		$products_info = $commonFunction->getSQLString($str);
		$product_description = $product_description." | ".$product_description_num." pcs | ".$commonFunction->getDeclaredPrice(floor($order_total),"dollar");

		//特殊字符处理
		$products_info = iconv('iso-8859-1','utf-8',$products_info);
		$customer_name = iconv('iso-8859-1','utf-8',$customer_name);
		$customer_address = iconv('iso-8859-1','utf-8',$customer_address);
		$products_info  = addslashes($products_info);
		$customer_name  = addslashes($customer_name);
		$customer_address  = addslashes($customer_address);

		//统计订单总数
		$sql_sum_orders = "select count(*) as sum_orders from orders where customer_email = '".$customer_email."' and (order_status = 20 or order_status = 30 or order_status = 40) group by customer_email";
		$result_sum_orders = $db->Execute($sql_sum_orders);
		if(!$result_sum_orders->EOF){
			$sum_orders = $result_sum_orders->fields['sum_orders'];
		}
		if($order_status == 20){ 
			$sum_orders = $sum_orders + 1;
		}

		$sql_insert_orders = "INSERT INTO orders
		(create_time,order_no,order_site,products_info,order_status,battery_flag,tracking_flag,payment_flag,customer_name,customer_telephone,customer_address,customer_postcode,customer_email,product_description,sum_orders) 
		values 
		(NOW(),'$order_no','$order_site','$products_info',$order_status,$battery_flag,$tracking_flag,$payment_flag,'$customer_name','$customer_telephone','$customer_address','$customer_postcode','$customer_email','$product_description',$sum_orders)";
		$db->Execute($sql_insert_orders);
	}else{
		$errorMsg = $order_id;
	}
}



//*************页面跳转到 订单列表***************
if($errorMsg == ""){
	$url = "orderList.php";
}else{
	$url = "orderList.php?errorMsg=".$errorMsg;
}
echo "<script   language='javascript' type ='text/javascript'>";
echo "window.location.href = '$url'"; //++++++++++++for test
echo "</script>";
?>


